﻿using System;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Sys.Filter;
using XCI.Sys.Forms;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Extensions;

namespace XCI.Windows
{
    //[System.ComponentModel.DesignerCategory("Code")]
    public partial class HistoryLogGrid : BaseUserControl
    {
        private string _tableName;
        private string _primaryKey;

        public HistoryLogGrid()
        {
            InitializeComponent();
            gridControl.ModelType = typeof(SysHistoryLog);
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        public async Task LoadAsync(string tableName, string primaryKey)
        {
            if (DesignMode) return;
            _tableName = tableName;
            _primaryKey = primaryKey;
            await gridControl.LoadFirstPageAsync();
        }

        private void gridControl_QueryData(object sender, XCI.Windows.LoadDataEventArgs e)
        {
            e.Task = async () =>
            {
                if (string.IsNullOrEmpty(_primaryKey))
                {
                    return new PagingList<SysHistoryLog>();
                }
                var filter = new SysHistoryLogFilter { TableName = _tableName, PrimaryKey = _primaryKey };
                filter.CombinePageFilter(e.PageFilter);
                return await SysLogService.Instance.SelectHistoryLogPageListAsync(filter);
            };
        }

        private void gridControl_DoubleClickRow(object sender, EventArgs e)
        {
            new FrmHistoryLogDetails(gridControl).ShowDialogOk();
        }
    }
}
